package cn.cvzhanshi.wechatpush.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * TODO 计算天数
 *
 * @author yupeng
 * @date 2022/8/23 11:24
 * @return
 */
public class DayUtils {

    public static int calculationBirthday(String clidate) {
        try {
            SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
            // 存今天
            Calendar cToday = Calendar.getInstance();
            // 存生日
            Calendar cBirth = Calendar.getInstance();
            // 设置生日
            cBirth.setTime(myFormatter.parse(clidate));
            // 修改为本年
            cBirth.set(Calendar.YEAR, cToday.get(Calendar.YEAR));
            int days;
            if (cBirth.get(Calendar.DAY_OF_YEAR) < cToday.get(Calendar.DAY_OF_YEAR)) {
                // 生日已经过了，要算明年的了
                days = cToday.getActualMaximum(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);
                days += cBirth.get(Calendar.DAY_OF_YEAR);
            } else {
                // 生日还没过
                days = cBirth.get(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);
            }
            // 输出结果
            if (days == 0) {
                return 0;
            } else {
                return days;
            }
        } catch (Exception e) {

        }
        return 0;
    }

    public static int calculationLianAi(String date) {
        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        int day = 0;
        try {
            long time = System.currentTimeMillis() - simpleDateFormat.parse(date).getTime();
            day = (int) (time / 86400000L);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day;
    }
}
